package com.ly.wxstore.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.wxstore.comm.MyPage;
import com.ly.wxstore.entity.PrizeUserinfo;
import com.ly.wxstore.entity.TicketRecord;
import com.ly.wxstore.entity.UserInfo;
import com.ly.wxstore.repository.PrizeUserinfoDao;
import com.ly.wxstore.repository.TicketRecordDao;
import com.ly.wxstore.repository.UserInfoDao;
import com.ly.wxstore.utils.PrizeUtils;

/**
 * 
 * 
 * @author Peter
 */
// Spring Service Bean的标识.
@Component
public class UserInfoService {

	private static Logger logger = LoggerFactory.getLogger(UserInfoService.class);

	@Autowired
	private UserInfoDao userInfoDao;

	@Autowired
	private TicketRecordDao ticketRecordDao;
	
	@Autowired
	private PrizeUserinfoDao prizeUserinfoDao;
	
	public UserInfo getById(Long id) {
		return userInfoDao.getById(id);
	}

	public List<UserInfo> getAll() {
		return userInfoDao.getAll();
	}

	/**
	 * 分页查询
	 * 
	 * @param overtime
	 * @param pageStart
	 * @param pageSize
	 * @return
	 */
	public MyPage<UserInfo> searchPage(UserInfo userInfo, int currentPage, int pageSize) {
		MyPage<UserInfo> myPage = new MyPage<UserInfo>();

		Long count = userInfoDao.searchCount(userInfo);

		int pageStart = (currentPage - 1) < 0 ? 0 : (currentPage - 1) * pageSize;
		List<UserInfo> list = userInfoDao.searchPage(userInfo, pageStart, pageSize);

		myPage.setCount(count);
		myPage.setContent(list);

		return myPage;
	}

	public PrizeUserinfo save(UserInfo userInfo) {
		
		//查询是否可以领取投票奖品
		PrizeUserinfo prizeUserinfo = new PrizeUserinfo();
		prizeUserinfo.setLevel(1L);
		prizeUserinfo.setPrize("50元油卡1张");
		prizeUserinfo.setReasonCode(0L);
		prizeUserinfo.setReason("初赛报名获奖");
		prizeUserinfo.setUid(userInfo.getUid());
		prizeUserinfo.setWinningDate(new Date());
		prizeUserinfoDao.save(prizeUserinfo);
		userInfoDao.save(userInfo);
		
		return prizeUserinfo;
	}

	public void update(UserInfo userInfo) {
		userInfoDao.update(userInfo);
	}

	/**
	 * 软删除
	 */
	public void delete(Long id) {
		userInfoDao.delete(id);
	}

	//-------------------------------------------------------------
	public UserInfo getByUid(String uid) {
		return userInfoDao.getByUid(uid);
	}

	/**
	 * 是否报过名
	 * @return
	 */
	public boolean isApplied(String uid) {
		UserInfo userInfo = userInfoDao.getByUid(uid);
		if(userInfo!=null){
			return true;
		}else{
			return false;
		}
	}
	
	//查询 根据名称或编号
	public List<UserInfo> searchByNameOrId(UserInfo userInfo){
		return userInfoDao.searchByNameOrId(userInfo);
	}
	
	//最新参考，根据ID倒叙
	public List<UserInfo> getAllOrderByIdDesc(){
		return userInfoDao.getAllOrderByIdDesc();
	}
	
	//查询 初赛 根据票数倒叙
	public List<UserInfo> getAllOrderByFirstTicketsDesc(){
		return userInfoDao.getAllOrderByFirstTicketsDesc();
	}
	
	//查询 决赛 根据票数倒叙
	public List<UserInfo> getAllOrderBySecondTicketsDesc(){
		return userInfoDao.getAllOrderBySecondTicketsDesc();
	}

	//查询今天已为他家投票的票数
	public Long searchRemainVoteForHome(String currentUid, String homeUid) {
		return userInfoDao.searchRemainVoteForHome(currentUid,homeUid);
	}
	
	//查询今天剩余的票数
	public Long searchRemainVote(String currentUid) {
		return userInfoDao.searchRemainVote(currentUid);
	}

	//初赛投票
	public PrizeUserinfo voteForFirst(String currentUid, String homeUid) {
		
		TicketRecord ticketRecord = new TicketRecord();
		ticketRecord.setCreateDate(new Date());
		ticketRecord.setUserUid(homeUid);
		ticketRecord.setVoterUid(currentUid);
		ticketRecord.setLevel(1L);
		
		ticketRecordDao.save(ticketRecord );
		
		//总票数+1
		userInfoDao.voteForFirst(homeUid);
		
		//查询是否可以领取投票奖品,报名获得过奖品了就不再获取奖品
		if(prizeUserinfoDao.getByUidAndReasonCodeForFirst(currentUid).intValue()==0){
			PrizeUserinfo prizeUserinfo = new PrizeUserinfo();
			prizeUserinfo.setLevel(1L);
			prizeUserinfo.setPrize(PrizeUtils.getPrizeName());
			prizeUserinfo.setReasonCode(1L);
			prizeUserinfo.setReason("第一次投票获奖");
			prizeUserinfo.setUid(currentUid);
			prizeUserinfo.setWinningDate(new Date());
			prizeUserinfoDao.save(prizeUserinfo);
			return prizeUserinfo;
		}else{
			return null;
		}
	}
	
	/**
	 * 参与总人数
	 * @return
	 */
	public Long totalForCanyu(){
		return userInfoDao.totalForCanyu(null);
	}
	/**
	 * 领奖人数
	 * @return
	 */
	public Long totalForReceivePrize(){
		return userInfoDao.totalForReceivePrize(null);
	}

}
